package org.hyjiacan.util.jcapture;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 鼠标事件监听�?
 * 
 * @author hyjiacan
 * 
 */
public class CaptureObserver implements MouseListener, MouseMotionListener {
	private Window win;// 程序窗口对象
	private Point oldpos;// 鼠标按下时的坐标
	private Point newpos;// 鼠标移动后的坐标
	private Rectangle rect;// 选中的区�?
	private long lastClick;// 上次按下鼠标左键的时�?

	public CaptureObserver() {
		rect = new Rectangle();
		lastClick = 0;
	}

	/**
	 * 通过�?��要监听的窗口创建�?��监听�?
	 * 
	 * @param win
	 *            要监听的窗口
	 */
	public CaptureObserver(Window win) {
		this();
		observe(win);
	}

	/**
	 * 设置要监听的窗口
	 * 
	 * @param win
	 *            要监听的窗口
	 */
	public void observe(Window win) {
		this.win = win;
		// 设置窗口的监听器
		win.setListener(this);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1) {// 鼠标左键
			if (e.getWhen() - lastClick < 500) { // 此次点击和上次点击时间间隔小�?00毫秒，则认为发生了双击事�?
				// 因为截图时坐标提示属于多余的，所以需要关�?
				win.hideTips();
				// �?��程序窗口
				win.getWindow().dispose();
				try {
					// 根据选择的区域截�?
					BufferedImage img = new Robot().createScreenCapture(win
							.getShotArea().getBounds());

					// 保存文件
					JFileChooser dlg = new JFileChooser();

					// 文件筛�?
					FileNameExtensionFilter filter;
					filter = new FileNameExtensionFilter("*.jpg | *.jpeg 文件",
							"*.jpg");
					dlg.addChoosableFileFilter(filter);
					filter = new FileNameExtensionFilter("*.png 文件", "*.png");
					dlg.setFileFilter(filter);
					dlg.addChoosableFileFilter(filter);
					filter = new FileNameExtensionFilter("*.bmp 文件", "*.bmp");
					dlg.addChoosableFileFilter(filter);
					
					if (dlg.showSaveDialog(win.getWindow()) == JFileChooser.APPROVE_OPTION) {
						String selectedFilter = dlg.getFileFilter().toString()
								.replaceAll("\\]", "");
						String ext = selectedFilter.substring(
								selectedFilter.lastIndexOf(".") + 1,
								selectedFilter.length());

						String selectedFileName = dlg.getSelectedFile()
								.getAbsolutePath();
						if (!selectedFileName.contains(".")) {
							selectedFileName += "." + ext;
						}else
						{
							ext = selectedFileName.substring(selectedFileName.lastIndexOf(".")+1, selectedFileName.length());						
						}
						// 写图片数据到硬盘
						ImageIO.write(img, ext, new File(selectedFileName));
					}
					win.setVisible(false);
					//System.exit(0);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}

			lastClick = e.getWhen();
		}

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// 在鼠标按下的时�?记录位置
		oldpos = e.getLocationOnScreen();
		rect.setLocation(oldpos.getLocation());
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// 设置坐标提示窗口的位�?
		win.setTipsPosition(e.getLocationOnScreen(), null);
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		
		// 获取新位�?
		newpos = e.getLocationOnScreen();

		if (newpos.x > oldpos.x && newpos.y > oldpos.y)// 右下方向
			rect.setSize(newpos.x - oldpos.x, newpos.y - oldpos.y);
		else if (newpos.x < oldpos.x && newpos.y > oldpos.y) {// 左下方向
			rect.setSize(oldpos.x - newpos.x, newpos.y - oldpos.y);
			// 重新定位
			rect.x = newpos.x;
		} else if (newpos.x < oldpos.x && newpos.y < oldpos.y) {// 左上方向
			rect.setSize(oldpos.x - newpos.x, oldpos.y - newpos.y);
			// 重新定位
			rect.setLocation(newpos);
		} else if (newpos.x > oldpos.x && newpos.y < oldpos.y) {// 右上方向
			rect.setSize(newpos.x - oldpos.x, oldpos.y - newpos.y);
			// 重新定位
			rect.y = newpos.y;
		}
		// 设置坐标提示窗口的位�?
		win.setTipsPosition(e.getLocationOnScreen(),rect);
		win.drawRect(rect);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// 设置坐标提示窗口的位�?
		win.setTipsPosition(e.getLocationOnScreen(), null);
	}
}